﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Configuration;

namespace ChurchData.Business
{
    public class ChurchDataBusinessBase
    {
        public string TableName { get; set; }

        protected string GetConnectionString()
        {
            System.Configuration.AppSettingsReader rdr = new System.Configuration.AppSettingsReader();

            string environment = rdr.GetValue("CurrentEnvironment", typeof(String)).ToString();
            return rdr.GetValue(environment + "ConnectionString", typeof(String)).ToString();
        }

        public static DataSet GetDataSet(string sql)
        {
            System.Configuration.AppSettingsReader rdr = new System.Configuration.AppSettingsReader();

            string environment = rdr.GetValue("CurrentEnvironment", typeof(String)).ToString();
            string strCon = rdr.GetValue(environment + "ConnectionString", typeof(String)).ToString();

            SqlConnection con = new SqlConnection(strCon);
            SqlDataAdapter adapt = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            con.Open();
            adapt.Fill(ds);
            con.Close();
            return ds;
        }

        public static void ExecuteNonQuery(string sql)
        {
            System.Configuration.AppSettingsReader rdr = new System.Configuration.AppSettingsReader();

            string environment = rdr.GetValue("CurrentEnvironment", typeof(String)).ToString();
            string strCon = rdr.GetValue(environment + "ConnectionString", typeof(String)).ToString();

            SqlConnection con = new SqlConnection(strCon);
            SqlCommand com = new SqlCommand(sql, con);
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
        }
    }
}
